<template>
	<view class="home">
		<!-- 自定义导航栏 -->
		<navbar />
		<tab :data="label" :tabIndex="tabIndex"  @tab="tab"/>
		
		<view class="home-list">
			<list :tab="label" @change="swiperCurrent" :labelIndex="labelIndex"></list>
		</view>
		
	</view>
</template>

<script>
	// uni-app 内置easyCOm '/components/组件名/组件名.vue' 则不需要引入直接使用
	import { get_label } from '@/common/api/index.js';
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				label: [],
				tabIndex: 0,
				labelIndex: 0
			}
		},
		computed: {
			...mapState(['userInfo'])
		},
		onLoad() {
			uni.$on('labelChanged', () => {
				this.getLabel()
			})
		},
		methods: {
			// 获取 tab 的 label
			getLabel() {
				get_label().then(res => {
					console.log('label',res)
					res.data.unshift({
						name: '全部'
					})
					this.label = res.data
				})
			},
			// tab 监听函数
			tab(data) {
				this.labelIndex = data.index
				console.log(data)
			},
			// swiper 监听函数
			swiperCurrent(index) {
				this.tabIndex = index
				this.labelIndex = index
			}
		},
		watch: {
			userInfo(newVal) {
				this.getLabel()
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
	}
	.home{
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: hidden;
		.home-list{
			flex: 1;
			height: 100%;
			box-sizing: border-box;
		}
	}
</style>
